package com.bocai.yuweiwangluo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.View.RoundImageView;
import com.bocai.yuweiwangluo.bean.BBSHomeBean;
import com.bocai.yuweiwangluo.utils.ImageLoaderUtil;
import com.bocai.yuweiwangluo.utils.TimeUtils;
import com.bocai.yuweiwangluo.utils.UrlData;

/**
 * 论坛列表的Adapter
 */
public class TalkListAdapter extends BaseAdapter{
    private Context context;
    private BBSHomeBean bbsHomeBean;
    public TalkListAdapter(Context context,BBSHomeBean bbsHomeBean){
        this.context=context;
        this.bbsHomeBean=bbsHomeBean;
    }
    @Override
    public int getCount() {
        return bbsHomeBean.getData().size();
    }

    @Override
    public Object getItem(int position) {
        return bbsHomeBean.getData().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView= LayoutInflater.from(context).inflate(R.layout.item_talk_list,null);
            viewHolder.ridTalkPhoto=(RoundImageView)convertView.findViewById(R.id.riv_talk_photo);
            viewHolder.tvTalkCount=(TextView)convertView.findViewById(R.id.tv_talk_count);
            viewHolder.tvTalkTime=(TextView)convertView.findViewById(R.id.tv_talk_time);
            viewHolder.tvTalkTitle=(TextView)convertView.findViewById(R.id.tv_talk_title);
            viewHolder.tvTalkName=(TextView)convertView.findViewById(R.id.tv_talk_name);
            viewHolder.tvTalkContent=(TextView)convertView.findViewById(R.id.tv_talk_content);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        BBSHomeBean.Data data=bbsHomeBean.getData().get(position);
        ImageLoaderUtil.loaderImage(UrlData.IMAGEURL+data.getIcon(),viewHolder.ridTalkPhoto,R.mipmap.photo_default);
        viewHolder.tvTalkContent.setText(data.getContent());
        viewHolder.tvTalkName.setText(data.getNickname());
        viewHolder.tvTalkCount.setText(data.getReply_count());
        viewHolder.tvTalkTitle.setText(data.getTitle());
        viewHolder.tvTalkTime.setText(TimeUtils.getTime_china(Long.parseLong(data.getTimeline())));
        return convertView;
    }


    public class ViewHolder {
        TextView tvTalkTitle;
        TextView tvTalkTime;
        TextView tvTalkContent;
        TextView tvTalkName;
        TextView tvTalkCount;
        RoundImageView ridTalkPhoto;
    }
}
